﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using haptic.src.engine;
using Microsoft.Xna.Framework;
using haptic.src.engine.render;
using haptic.src.engine.core;
using haptic.src.engine.scene;
using System.Diagnostics;
using haptic.src.engine.collisions;

namespace haptic.src.components
{
    [Serializable]
    public class cSceneGraph : Component
    {
        #region Constructors
        public cSceneGraph(){}
        public cSceneGraph(Entity oOwner) : base(oOwner) {}
        #endregion

        #region Load
        public override Result Load(Scene oScene, Microsoft.Xna.Framework.Content.ContentManager oContent)
        {
            Debug.Assert(oScene != null);
            if (m_oNode != null)
            {
                Result oRes = m_oNode.Load();
                if (oRes.IsFailed())
                    return oRes;
                if (m_oNode.eType == Node.NodeType.E_ROOT)
                {
                    RootNode oRootNode = (RootNode)m_oNode;
                    if (oRootNode.m_oCollisionNodes != null)
                    {
                        foreach (CollisionNode oColNode in oRootNode.m_oCollisionNodes)
                        {
                            if (oColNode.oCollider != null)
                                oScene.m_oCollisionManager.AddCollider(oColNode.oCollider, CollisionManager.ColliderFlags.E_DEFAULT);
                        }
                    }
                }
                else if (m_oNode.eType == Node.NodeType.E_COLLISION)
                {
                    CollisionNode oColNode = (CollisionNode)m_oNode;
                    if (oColNode.oCollider != null)
                        oScene.m_oCollisionManager.AddCollider(oColNode.oCollider, CollisionManager.ColliderFlags.E_DEFAULT);
                }
            }
            
            return Result.Success;
        }
        #endregion

        #region Update
        public override void Update(GameTime oTime)
        {
            if (m_oNode != null)
            {
                m_oNode.SetRootPos(m_oEntity.m_vPosition);
                m_oNode.Update(oTime);
            }
            base.Update(oTime);
        }
        #endregion

        #region Draw
        public override void Draw(Renderer oRenderer, GameTime oTime)
        {
            if (m_oNode != null)
                m_oNode.Draw(oRenderer);
        }
        public override void  DrawDebug(haptic.src.engine.debug.DebugRenderer oDebugRenderer, GameTime oTime)
        {
             if (m_oNode != null)
                 m_oNode.DrawDebug(oDebugRenderer);
        }
        #endregion

        #region Attributes
        public string m_sSceneGraphPath;
        
        public Node m_oNode;
        #endregion
    }
}
